#!/bin/bash
#
# SADFBI v1.1 - a SADF wrapper for metrics elaboration on BI tools
# ----------------------------------------------------------------
#
# Copyright 2016 Caglio Giuliano
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#




# SAR log archive folder
# please customize this for your system
sa_archive_folder=/var/log/sa


#LC_ALL locale
out_LCALL="it_IT"


### SAR PARAMETERS AND METRICS
###   For reference only, if your sar version has different parameters with different description
###   you have to modify this arrays to  reflect  correct  options  used  for  the  SADF  command
###
###   You could also customize output file parameters and description as you prefer.

# SADF command line parameters
ALL_SAR_METRICS_PARM=("-b" "-B" "-c" "-d" "-I ALL" "-n DEV" "-n EDEV" "-P ALL" "-q" "-r" "-R" "-W")
# SADF metric names (related to SADF command line parameters ALL_SAL_METRICS_PARM)
ALL_SAR_METRICS_NAME=("io transfer rate" "paging stats" "process creation rate" "block device activity" "interrupts" "network" "network error" "cpu" "queue and load" "memory and swapspace" "memory" "swapspace")
# SADF metric output filenames (related to SADF command line parameters ALL_SAL_METRICS_PARM)
ALL_SAR_METRICS_FILENAME=("io_transfer_rate.csv" "paging.csv" "proc_creation_rate.csv" "block_dev_activity.csv" "interrupts.csv" "network_activity.csv" "network_errors.csv" "cpu.csv" "queue_load.csv" "memory_swapspace.csv" "memory.csv" "swapspace.csv")

# -----------------------   end user customizable part -----------------------

old_LCALL=$LC_ALL
export LC_ALL=$out_LCALL


eval metrics_params=('"${ALL_SAR_METRICS_PARM[@]}"')
eval metrics_names=('"${ALL_SAR_METRICS_NAME[@]}"')
eval metrics_files=('"${ALL_SAR_METRICS_FILENAME[@]}"')

sar_cmd_line_param=''
sarbi_cmd_line_output_folder=$PWD

if [ $# -eq 0 ] || [ $1 == "-h" ] || [ $1 == "--help" ] || [ $1 == "-?" ]; then
  echo "USAGE : $0 [sadf_metric_parameter] /path/to/output/folder"
  echo
  echo "  [sadf_metric_parameter]   could be one of the following :"
  for ((m=${#metrics_params[@]}-1; m>=0 ; m--))
  do
    metric_description=${metrics_names[$m]}
    metric_param=${metrics_params[$m]}

    echo -e "      $metric_param\t$metric_description"
  done
  echo
  echo "  Note : if you explicit the sadf metric, $0 will only "
  echo "         produce one output file, for that metric."
  echo
  echo "         If you don't explicit the metric parameter $0"
  echo "         will generate multiple files, one for each metric."
  echo
  echo

  exit 1
elif [ $# -eq 1 ]; then
  sarbi_cmd_line_output_folder=$1
elif [ $# -eq 2 ]; then
  sarbi_cmd_line_output_folder=$2
  sar_cmd_line_param=$1
fi




### SAR PARAMETERS AND METRICS ###
###   for reference only, if your sar version has different parameters with different description
###   you have to modify the arrays at the beginning of the script to reflect correct options used
###   and, if you want to track down all the parameters meaning, you could update also this reference.
###
# -b                                            Report I/O and transfer rate statistics.
# -B                                            Report paging statistics. The following values are displayed:
# -c                                            Report process creation activity.
# -d                                            Report activity for each block device (kernels 2.4 and newer only).
# -I { irq | SUM | ALL | XALL }                 Report statistics for a given interrupt.
# -n { DEV | EDEV | NFS | NFSD | SOCK | ALL }   Report network statistics.
# -P { cpu | ALL }                              Report per-processor statistics for the specified processor or processors.
#                                               Specifying the ALL keyword reports statistics for each individual processor,
#                                               and globally for all processors.
# -q                                            Report queue length and load averages. The following values are displayed:
# -r                                            Report memory and swap space utilization statistics.
# -R                                            Report memory statistics
# -W                                            Report swap space utilization statistics.



# -------------------------------------



for sa_file in $(ls -1 $sa_archive_folder/)
do

   is_data_type=$(file $sa_archive_folder/$sa_file  | awk -F ":" '{ print $2}' | grep data | wc -l | bc )
   if [ $is_data_type -ge 1 ]; then
      echo "INFO : working on archive $sa_archive_folder/$sa_file "


      if [ ! "x$sar_cmd_line_param" == 'x' ]; then
        ALL_SAR_METRICS_PARM=( "$sar_cmd_line_param" )
        eval metrics_params=('"${ALL_SAR_METRICS_PARM[@]}"')
      fi


      for ((m=${#metrics_params[@]}-1; m>=0 ; m--))
      do
         metric_parm=${metrics_params[$m]}
         metric_name=${metrics_names[$m]}
         metric_file=${metrics_files[$m]}

         echo "INFO : working on metic - $metric_name ( $metric_parm )"

         # test if metric is available
         sar $metric_parm -f $sa_archive_folder/$sa_file  >/dev/null 2>&1
         retval=$?

         if [ $retval -gt 0 ]; then
           echo "ERROR : metric $metric_parm is not present."
         else
           echo "INFO : metric $metric_parm found. Extracting CSV in   $sarbi_cmd_line_output_folder/$metric_file"
         fi

         # dump headers if file does not exists
         if [ ! -f $sarbi_cmd_line_output_folder/$metric_file ]; then
           echo "overwrite......."
           sar $metric_parm 1 1| head -n 5 | awk 'BEGIN{ is_blank=0 }{ if (is_blank>0){ line="hostname;interval;datetime";  for (i=3; i<=NF; i++){ line=line ";" $i; }  is_blank=0; print line; };   if ( length($0)==0 ){ is_blank=1; }   }' > $sarbi_cmd_line_output_folder/$metric_file
         fi

         # dump sa archive content to file
         if [ $retval -eq 0 ]; then
            sadf -t -d $sa_archive_folder/$sa_file -- $metric_parm >> $sarbi_cmd_line_output_folder/$metric_file
         fi

      done
   fi
done

LC_ALL=$old_LCALL
